import lightgbm as lgb
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt

# 1. 读取数据集
train_data = pd.read_csv('dataset/train.csv')
test_data = pd.read_csv('dataset/test.csv')

# 2. 数据预处理
X_train = train_data.drop(['id', 'target'], axis=1)
y_train = train_data['target']

# 3. 切分训练集和验证集
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2, random_state=42)

# 4. 创建并训练模型
model = lgb.LGBMClassifier(boosting_type='gbdt',
                           num_leaves=31,
                           learning_rate=0.05,
                           n_estimators=100,
                           objective='binary',
                           metric='binary_logloss')

eval_set = [(X_val, y_val)]  # 用于验证模型的数据集

model.fit(X_train, y_train,
          eval_set=eval_set,
          early_stopping_rounds=10,
          verbose=10)

# 绘制准确率和损失曲线
train_metric = model.evals_result_['binary_logloss']  # 训练集上的损失值
val_metric = model.evals_result_['valid_0']['binary_logloss']  # 验证集上的损失值

train_accuracy = [accuracy_score(y_train, model.predict(X_train))]
val_accuracy = [accuracy_score(y_val, model.predict(X_val))]

for i in range(1, len(train_metric)):
    train_accuracy.append(accuracy_score(y_train, model.predict(X_train, num_iteration=i)))
    val_accuracy.append(accuracy_score(y_val, model.predict(X_val, num_iteration=i)))

plt.figure(figsize=(12, 6))
plt.subplot(1, 2, 1)
plt.plot(train_metric, label='Training Loss')
plt.plot(val_metric, label='Validation Loss')
plt.xlabel('Iterations')
plt.ylabel('Binary Logloss')
plt.title('Training and Validation Loss')
plt.legend()

plt.subplot(1, 2, 2)
plt.plot(train_accuracy, label='Training Accuracy')
plt.plot(val_accuracy, label='Validation Accuracy')
plt.xlabel('Iterations')
plt.ylabel('Accuracy')
plt.title('Training and Validation Accuracy')
plt.legend()

plt.tight_layout()
plt.show()

# 5. 预测结果
y_pred = model.predict(test_data.drop(['id'], axis=1))

# 6. 保存预测结果为CSV文件
pd.DataFrame({'id': test_data['id'], 'target': y_pred}).to_csv('result/LGBM.csv', index=False)
print('&#8203;``oaicite:{"number":1,"invalid_reason":"Malformed citation 【预测结果已输出为CSV文件】"}``&#8203;')
